const expres=require('express')
const utils=require('utility')
const Router=expres.Router()
const model=require('./model')
const UserTable=model.getModel('user_table')
const TokenTable=model.getModel('token_table')
// const allowOrigin=require('./middleware/allowOrigin')

const _filter={'pwd':0,'__v':0}


Router.get('/test',(req,res)=>{

  return res.json({test:1})
})
Router.post('/get-user',(req,res)=>{
  const {token}=req.body

  TokenTable.findOne({token},_filter,function (err, doc) {
    if(!doc){
      return res.json({code:1,msg:'token已时效,请重新登录'})
    }
    const {userId}=doc
    UserTable.findOne({_id:userId},_filter,function(e,d) {
      if(!d){
        return res.json({code:1,msg:'该用户不存在'})
      }
      return res.json({code:0, data:d})
    })
  })
})


Router.post('/get-info',(req,res)=>{
  const {_id}=req.body
  const token=req.header('token')

  TokenTable.findOne({token},_filter,function (err, doc) {
    if(!doc){
      return res.json({code:1,msg:'token已时效,请重新登录'})
    }

    UserTable.findOne({_id},_filter,function(e,d) {
      if(!d){
        return res.json({code:1,msg:'该用户不存在'})
      }
      return res.json({code:0, data:d})
    })
  })
})

Router.post('/edit-user',(req,res)=>{
  const {_id,username,desc}=req.body

  const token=req.header('token')

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }
    UserTable.findOneAndUpdate({_id},{username,desc},function(e1,d1){
      if(e1){
        return res.json({code:1,msg:'后端出错了'})
      }
      return res.json({code:0,data:d1})
    })
  })
})


Router.post('/edit-pwd',(req,res)=>{
  const {_id,pwd}=req.body

  const token=req.header('token')

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }
    UserTable.findOneAndUpdate({_id},{pwd:md5Pwd(pwd)},function(e1,d1){
      if(e1){
        return res.json({code:1,msg:'后端出错了'})
      }
      return res.json({code:0,data:d1})
    })
  })
})

Router.post('/login',(req,res)=>{
  const {user,pwd}=req.body
  UserTable.findOne({user,pwd:md5Pwd(pwd)},_filter,function(err,doc){
    if(!doc){
      return res.json({code:1,msg:'用户名不存在或者密码错误'})
    }
    const {user,_id,username,desc}=doc
    const tokenModel=new TokenTable({
      token:setToken(user,_id),
      userId:_id.toString()
    })
    tokenModel.save(function(e,d) {
      if(e){
        return res.json({code:1,msg:'后端出错了'})
      }
      const {token}=d
      return res.json({code:0,data:{token,user:{user,_id,username,desc}}})
    })
  })
})

Router.post('/register',(req,res)=>{

  const {user,username,pwd,desc}=req.body
  console.log(user,username,pwd,desc)
  UserTable.findOne({user},(err,doc)=>{
    if(doc){
      return res.json({code:1,msg:'用户名重复'})
    }
    const userModel=new UserTable({user,username,desc,pwd:md5Pwd(pwd)})
    userModel.save(function(e,d) {
      if(e){
        return res.json({code:1,msg:'后端出错了'})
      }
      const {user,_id,username,desc}=d
      const tokenModel=new TokenTable({
        token:setToken(user,_id),
        userId:_id.toString()
      })
      tokenModel.save(function(e,d) {
        if(e){
          return res.json({code:1,msg:'后端出错了'})
        }
        const {token}=d
        return res.json({code:0,data:{token,user:{user,_id,username,desc}}})
      })
    })
  })
})

function md5Pwd(pwd){
  const salt='vicky_is_good_3957x8yza6!@#IUHJH~~'
  return utils.md5(utils.md5(pwd+salt))
}

function setToken(user,_id){
  const salt='vicky_is_good_3957x8yza6!@#IUHJH~~'
  return utils.md5(utils.md5(user+_id+salt))
}

module.exports=Router
